package com.dc.web.admin;

import com.dc.po.Type;
import com.dc.service.Impl.TypeServiceImpl;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private TypeServiceImpl typeService;

    //前往分类列表
    @GetMapping("/types") //配置分页数据参数  每页默认10条，id倒序排列
    public String types(@PageableDefault(size = 6,sort = {"id"},direction = Sort.Direction.DESC)
                                    Pageable pageable, Model model){
        model.addAttribute("page",typeService.listType(pageable));
        return "admin/types";
    }

    //前往新增分类页
    @GetMapping("/types/input")
    public String input(Model model) {
        model.addAttribute("type", new Type());
        return "admin/types-input";
    }

    //前往编辑分类页
    @GetMapping("/types/{id}/input")
    public String editInput(@PathVariable Long id,
                            Model model) {
        model.addAttribute("type", typeService.getType(id));
        return "admin/types-input";
    }


    //新增分类
    @PostMapping("/types")
    public String save(@Valid Type type, BindingResult result, RedirectAttributes attributes){
        /*查询表中分类的name是否存在*/
        Type type1 = typeService.getTypeByName(type.getName());
        /*自定义返回的错误信息 name绑定实体类要检验的属性名称
         @NotBlank(message = "分类名称不能为空")private String name;*/
        if (type1 != null) {
            result.rejectValue("name","nameError","不能添加重复的分类");
        }
        /*传递过来的name属性为空或者名字重复 返回页面*/
        if (result.hasErrors()) {
            return "admin/types-input";
        }

        try {
            Type t = typeService.saveType(type);
            attributes.addFlashAttribute("typeMsg","操作成功，新增"+type.getName()+"分类");
        } catch (Exception e) {
            System.out.println("========saveType========"+e);
            e.printStackTrace();
            attributes.addFlashAttribute("typeMsg","操作失败");
        }finally {
            return "redirect:/admin/types";
        }
    }




    //修改分类
    @PostMapping("/types/{id}")//实体校验要跟绑定结果放一起
    public String editPost(@Valid Type type, BindingResult result,
                           @PathVariable Long id,
                           RedirectAttributes attributes) {
        //检查是否有同名分类
        Type type1 = typeService.getTypeByName(type.getName());
        if (type1 != null) {
            result.rejectValue("name","nameError","不能添加重复的分类");
        }
        //后端对名称属性的非空校验
        if (result.hasErrors()) {
            return "admin/types-input";
        }
        //执行更新操作 这里被业务层复杂化了，用dao层的save操作更好
        Type t = typeService.updateType(id,type);
        if (t == null ) {
            attributes.addFlashAttribute("typeMsg", "更新失败");
        } else {
            attributes.addFlashAttribute("typeMsg", "更新成功");
        }
        return "redirect:/admin/types";
    }


    @GetMapping("/types/{id}/delete")
    public String delete(@PathVariable Long id,
                         RedirectAttributes attributes) {
        typeService.deleteType(id);
        attributes.addFlashAttribute("typeMsg", "删除成功");
        return "redirect:/admin/types";
    }
}
